package com.smm.session;

import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * 自定义的sessionManger
 */
public class SessionManager extends DefaultWebSessionManager {
    /**
     * 指定sessionID的获取方式
     */
    protected Serializable getSessionId(ServletRequest request, ServletResponse response){
        String header = WebUtils.toHttp(request).getHeader("");
        if (StringUtils.isEmpty(header)){
            return super.getSessionId(request,response);
        }else {
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE,"header");
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID,header);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID,Boolean.TRUE);
            return header;
        }
    }
}
